package com.wuhao.shootclient.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;

/**
 * 轨道服务
 */
public class CameraService extends Service {

    //ip地址
    private String ip1 = "192.168.0.190";
    private String ip2 = "192.168.0.191";
    //端口号
    private int port = 8899;

    private int number1 = 0;

    private int number2 = 0;

    private String receiveMsg;

    private BufferedReader in;

    //3500为一米的距离
    private int distance = 14000;

    private int time = 30;

    public CameraService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int Identification = intent.getIntExtra("Identification", 0);

        SocketClient(Identification);
        if (Identification != 6){
            delayed();
        }

        return super.onStartCommand(intent, flags, startId);
    }


    /**
     * 3500大概为一米
     * @param type
     */
    public void SocketClient(int type) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Socket socket = null;
                    socket = new Socket(ip1, port);
                    Writer writer = new OutputStreamWriter(socket.getOutputStream());
                    Socket socket2 = null;
                    socket2 = new Socket(ip2,port);
                    Writer writer2 = new OutputStreamWriter(socket2.getOutputStream());

                    writer.write(1 + " dev\n");
                    writer.flush();
                    Thread.sleep(50);
                    writer.write(1 + " ena\n");
                    writer.flush();
                    Thread.sleep(50);
                    writer.write(1 + " cfg crh=2.0\n");
                    writer.flush();
                    Thread.sleep(50);

                    writer2.write(2 + " dev\n");
                    writer2.flush();
                    Thread.sleep(50);
                    writer2.write(2 + " ena\n");
                    writer2.flush();
                    Thread.sleep(50);
                    writer2.write(2 + " cfg crh=2.0\n");
                    writer2.flush();
                    Thread.sleep(50);

                    //移动
                    if ( type == 3 || type == 5){
                        number1 = -distance*type;
                        number2 = number1+3*distance;
                        //线程延时 防止过传感器时停止
                        if (type == 1 && number1 == 0){
                            writer.write(1 + " pos " + number1 + "\n");
                            writer.flush();
                            Thread.sleep(50);
                        }
                        else {
                            writer.write(1 + " pos " + number1 + "\n");
                            writer.flush();
                            Thread.sleep(50);
                        }
                        if (number2 <= 0){
                            writer2.write(2 + " pos " + number2 + "\n");
                            writer2.flush();
                            Thread.sleep(50);
                        }
                        //延时    移动到指定位置一段时间后松刹车
                    }
                    if (type == 4){
                        writer.write(1 + " pos " + 0 + "\n");
                        writer.flush();
                        Thread.sleep(50);
                        writer2.write(2 + " pos " + distance + "\n");
                        writer2.flush();
                        Thread.sleep(50);
                    }
                    //当前位置归零
                    if (type == 6){
                        number1 = 0;
                        number2 = 0;
                        writer.write(1 + " off\n");
                        writer.flush();
                        Thread.sleep(50);
                        writer2.write(2 + " off\n");
                        writer2.flush();
                        Thread.sleep(50);
                    }
                    //执行完操作后在一定时间后松开刹车
                    else if (type == 7){
                        writer.write(1 + " cfg crh=0.0\n");
                        writer.flush();
                        Thread.sleep(50);
                        writer2.write(2 + " cfg crh=0.0\n");
                        writer2.flush();
                        Thread.sleep(50);
                    }

//                    Log.e("=========TAG",number1+"==number1");
//                    Log.e("=========TAG",number2+"==number2");
                    socket.close();

                } catch (IOException e) {
//                    Log.i("=========TAG", "IOException" + e.toString());
                    e.printStackTrace();
                } catch (InterruptedException e) {
//                    Log.i("=========TAG", "printStackTrace" + e.toString());
                    e.printStackTrace();
                }
            }
        }.start();

    }

    /**
     * 线程   在规定时间没有运行轨道   则使电机拖能
     */
    private void  delayed(){
        Thread thread = new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    time = 30;
                    while (time > -1) {
                        Thread.sleep(1000);
                        time--;
//                        Log.e("=========TAG",time+"==time");
                        if (time == 0){
                            SocketClient(7);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        thread.interrupt();
        thread.start();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
}
